package com.ysqcould.message.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.ysqcould.allocation.domain.MessageConfig;
import com.ysqcould.allocation.domain.WxConfig;
import com.ysqcould.allocation.mapper.MessageConfigMapper;
import com.ysqcould.allocation.mapper.WxConfigMapper;
import com.ysqcould.common.core.domain.entity.JbUser;
import com.ysqcould.common.utils.DateUtils;
import com.ysqcould.content.domain.Press;
import com.ysqcould.content.mapper.PressMapper;
import com.ysqcould.message.domain.WxMessage;
import com.ysqcould.message.domain.WxMessagePress;
import com.ysqcould.message.mapper.WxMessageMapper;
import com.ysqcould.message.mapper.WxMessagePressMapper;
import com.ysqcould.message.service.IWxMessagePressService;
import com.ysqcould.message.utils.wxmessage.MessageUtil;
import com.ysqcould.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.ysqcould.common.utils.SecurityUtils.getUserId;

/**
 * 系统消息Service业务层处理
 * 
 * @author jcd
 * @date 2022-09-23
 */
@Service
public class WxMessagePressServiceImpl implements IWxMessagePressService
{
    @Autowired
    private WxMessagePressMapper wxMessagePressMapper;

    @Autowired
    private PressMapper pressMapper;

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private WxConfigMapper wxConfigMapper;

    @Resource
    private WxMessageMapper wxMessageMapper;

    @Autowired
    private MessageConfigMapper messageConfigMapper;

   static Long typeId=5L;

    /**
     * 查询系统消息
     * 
     * @param id 系统消息主键
     * @return 系统消息
     */
    @Override
    public WxMessagePress selectWxMessagePressById(Long id)
    {
        final WxMessagePress wxMessagePress = wxMessagePressMapper.selectWxMessagePressById(id);
         if (ObjectUtil.isEmpty(wxMessagePress)){
             WxMessagePress messagePress=new WxMessagePress();
             String str="您有一份文件送达，请及时查阅！";
             final Press press = pressMapper.selectPressById(id);
             final JbUser user = userMapper.selectUserById(press.getAsId());
             final WxConfig wxConfig = wxConfigMapper.selectWxConfigById(press.getAsId());
             messagePress.setFirstData(str);
             messagePress.setKeyword1Data(press.getName());
             messagePress.setKeyword2Data(user.getName());
             messagePress.setKeyword3Data(DateUtils.getTime());
             messagePress.setRemark(press.getTag());
             StringBuilder bu=new StringBuilder("https://www.shequncloud.com/wxapp/?");
             bu.append("asId=").append(press.getAsId());
             bu.append("&appId=").append(wxConfig.getAppId());
             bu.append("&state=").append(press.getAsId());
             bu.append("#/pages/newsDetail/normalNew?");
             bu.append("id=").append(id);
             messagePress.setLinkUrl(bu.toString());
             return messagePress;
         }else {
             return wxMessagePress;
         }
    }

    /**
     * 查询系统消息列表
     * 
     * @param wxMessagePress 系统消息
     * @return 系统消息
     */
    @Override
    public List<WxMessagePress> selectWxMessagePressList(WxMessagePress wxMessagePress)
    {
        return wxMessagePressMapper.selectWxMessagePressList(wxMessagePress);
    }

    /**
     * 新增系统消息
     * 
     * @param wxMessagePress 系统消息
     * @return 结果
     */
    @Override
    public int insertWxMessagePress(WxMessagePress wxMessagePress)
    {

        wxMessagePress.setCreateTime(DateUtils.getNowDate());
        final MessageConfig wxMessage = messageConfigMapper.getMessageConfig(getUserId(), typeId);
        MessageUtil.sendPressMessage(getUserId(),wxMessage,wxMessagePress);
        return wxMessagePressMapper.insertWxMessagePress(wxMessagePress);
    }

    /**
     * 修改系统消息
     * 
     * @param wxMessagePress 系统消息
     * @return 结果
     */
    @Override
    public int updateWxMessagePress(WxMessagePress wxMessagePress)
    {
        wxMessagePress.setUpdateTime(DateUtils.getNowDate());
        final MessageConfig wxMessage = messageConfigMapper.getMessageConfig(getUserId(), typeId);
        MessageUtil.sendPressMessage(getUserId(),wxMessage,wxMessagePress);
        return wxMessagePressMapper.updateWxMessagePress(wxMessagePress);
    }

    /**
     * 批量删除系统消息
     * 
     * @param ids 需要删除的系统消息主键
     * @return 结果
     */
    @Override
    public int deleteWxMessagePressByIds(Long[] ids)
    {
        return wxMessagePressMapper.deleteWxMessagePressByIds(ids);
    }

    /**
     * 删除系统消息信息
     * 
     * @param id 系统消息主键
     * @return 结果
     */
    @Override
    public int deleteWxMessagePressById(Long id)
    {
        return wxMessagePressMapper.deleteWxMessagePressById(id);
    }
}
